package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bouncycastle.crypto.io.MacOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.ApiT_member;
import cn.com.api.entity.ApiU_circle;
import cn.com.api.entity.ApiU_circle_member;
import cn.com.api.entity.ApiU_message;
import cn.com.api.entity.ApiU_user_friend_group;
import cn.com.api.entity.ApiU_user_friend_group_detail;
import cn.com.api.mapper.ApiT_memberMapper;
import cn.com.api.mapper.ApiU_circleMapper;
import cn.com.api.mapper.ApiU_circle_memberMapper;
import cn.com.api.mapper.ApiU_messageMapper;
import cn.com.api.mapper.ApiU_user_friend_groupMapper;
import cn.com.api.mapper.ApiU_user_friend_group_detailMapper;
import cn.com.api.service.ApiU_circle_memberService;
import cn.com.api.service.ApiU_user_friend_group_detailService;
import cn.com.api.service.impl.base.U_circle_memberServiceImplBase;
import cn.com.util.DateUtil;
import cn.com.util.PageInfo;
import cn.com.util.Util;
import cn.com.util.Utils;

@Service
public class ApiU_circle_memberServiceImpl extends U_circle_memberServiceImplBase implements ApiU_circle_memberService {
	@Autowired
	private ApiU_circle_memberMapper apiU_circle_memberMapper;
	@Autowired
	private ApiU_circleMapper apiU_circleMapper;
	@Autowired
	private ApiT_memberMapper apiT_memberMapper;
	@Autowired
	private ApiU_messageMapper apiU_messageMapper;
	@Autowired
	private ApiU_user_friend_groupMapper apiU_user_friend_groupMapper;
	@Autowired
	private ApiU_user_friend_group_detailMapper apiU_user_friend_group_detailMapper;

	// 新增对象后返回记录ID
	public int U_circle_memberInsertion(ApiU_circle_member u_circle_member) {
		return this.apiU_circle_memberMapper.U_circle_memberInsertion(u_circle_member);
	}

	public List<Map<String, Object>> findU_circle_membersByMap(HashMap condition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ApiU_circle_member> getcirclemembersListPage(Map condition, PageInfo pageInfo) {
		return this.apiU_circle_memberMapper.getcirclemembersListPage(condition, pageInfo);
	}

	public Integer getcircle_addedcount(String user_id) {
		return this.apiU_circle_memberMapper.getcircle_addedcount(user_id);
	}

	public void updatecirclememberinfo(Map condition) {
		this.apiU_circle_memberMapper.updatecirclememberinfo(condition);
	}

	public void insertcirclememberList(List<ApiU_circle_member> circlememberlist) {
		this.apiU_circle_memberMapper.insertcirclememberList(circlememberlist);
	}

	public ApiU_circle_member getcirclememberInfo(Map condition) {
		return this.apiU_circle_memberMapper.getcirclememberInfo(condition);
	}

	public Integer isjoincircle(Map paramMap) {
		Integer result = -1;
		PageInfo pageInfo = new PageInfo();
		List<ApiU_circle_member> list = apiU_circle_memberMapper.getcirclemembersListPage(paramMap, pageInfo);
		if (list != null && list.size() > 0) {
			ApiU_circle_member apiU_circle_member = list.get(0);
			result = apiU_circle_member.getStatus();
		}
		return result;
	}

	public Integer assignmentcircleadmin(String user_id, String assignor_id, String circle_id) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("user_id", user_id);
		map.put("circle_id", circle_id);
		ApiU_circle_member apiU_circle_member = apiU_circle_memberMapper.getcirclememberInfo(map);
		if (apiU_circle_member.getIs_admin() == 1) {
			apiU_circle_member.setIs_admin(0);
			apiU_circle_memberMapper.updateU_circle_member(apiU_circle_member);
			map.clear();
			map.put("user_id", assignor_id);
			map.put("circle_id", circle_id);
			ApiU_circle_member assignor = apiU_circle_memberMapper.getcirclememberInfo(map);
			assignor.setIs_admin(1);
			apiU_circle_memberMapper.updateU_circle_member(assignor);
			// 修改圈子创建人信息
			ApiU_circle apiU_circle = apiU_circleMapper.findU_circleById(Integer.valueOf(circle_id));
			apiU_circle.setCreate_uid(assignor_id);
			apiU_circleMapper.updateU_circle(apiU_circle);
			return 1;
		} else {// 不是圈主
			return -1;
		}
	}

	public Map<String, Object> joincircle(String user_id, String joinuserids, String circle_id, String apply_msg)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		ApiU_circle apiU_circle = apiU_circleMapper.findU_circleById(Integer.valueOf(circle_id));
		Integer isneedconfirm = apiU_circle.getIs_need_confirm();
		String[] userids = joinuserids.split(":");
		// 消息信息存放list，用于最后批量提交
		List<ApiU_message> messagelist = new ArrayList<ApiU_message>();
		for (String joinuserid : userids) {
			ApiT_member join_member = apiT_memberMapper.findT_memberById(Integer.valueOf(joinuserid));
			map.clear();
			map.put("user_id", joinuserid);
			map.put("circle_id", circle_id);
			ApiU_circle_member apiU_circle_member = apiU_circle_memberMapper.getcirclememberInfo(map);
			map.clear();
			ApiT_member apiT_member = apiT_memberMapper.findT_memberById(Integer.valueOf(user_id));
			if (apiU_circle_member != null) {// 已有申请资料
				if (apiU_circle_member.getStatus() == 2 || apiU_circle_member.getStatus() == 3
						|| apiU_circle_member.getStatus() == 4 || apiU_circle_member.getStatus() == 0) {// 之前请求是2拒绝；3退出；4被清除，可以再次申请
					apiU_circle_member.setAdd_time(DateUtil.nowTimeToString());
					apiU_circle_member.setIs_deal(0);
					apiU_circle_member.setIs_silent(0);
					apiU_circle_member.setUpdate_time("");
					apiU_circle_member.setApply_msg(apply_msg);
					if (isneedconfirm == 0) {// 不需要群主同意，直接加入圈子
						apiU_circle_member.setStatus(1);
					} else {
						apiU_circle_member.setStatus(0);
					}
					apiU_circle_memberMapper.updateU_circle_member(apiU_circle_member);
				} else {// 已经是圈子成员
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "您已是该圈子的成员");
				}
			} else {// 未申请过
				apiU_circle_member = new ApiU_circle_member();
				apiU_circle_member.setUser_id(joinuserid);
				apiU_circle_member.setAdd_time(DateUtil.nowTimeToString());
				apiU_circle_member.setIs_admin(0);
				apiU_circle_member.setIs_deal(0);
				apiU_circle_member.setIs_silent(0);
				apiU_circle_member.setApply_msg(apply_msg);
				apiU_circle_member.setNick_name(join_member.getC_nick());
				apiU_circle_member.setCircle_id(circle_id);
				if (isneedconfirm == 0) {// 不需要群主同意，直接加入圈子
					apiU_circle_member.setStatus(1);
				} else {
					apiU_circle_member.setStatus(0);
				}
				apiU_circle_memberMapper.U_circle_memberInsertion(apiU_circle_member);
			}

			// 申请成功，在系统消息中加入申请人提醒记录
			if (!joinuserid.equals(user_id)) {// 邀请别人加入，发消息给别人
				ApiU_message message = new ApiU_message();
				message.setAdd_time(DateUtil.nowTimeToString());
				message.setConn_id(apiU_circle_member.getId());
				message.setContent("你的好友" + apiT_member.getC_nick() + "邀请你加入" + apiU_circle.getCircle_name() + "圈子");
				message.setTitle("邀请加入圈子");
				message.setType(8);
				message.setUser_id(joinuserid);
				message.setIs_read(0);
				messagelist.add(message);
			}

			map.clear();
			map.put("is_admin", 1);
			map.put("circle_id", circle_id);
			// 圈主
			ApiU_circle_member qzCircle_member = apiU_circle_memberMapper.getcirclememberInfo(map);
			map.clear();
			// 系统消息中加入提醒圈主的信息
			ApiU_message qzmessage = new ApiU_message();
			qzmessage.setAdd_time(DateUtil.nowTimeToString());
			qzmessage.setConn_id(apiU_circle_member.getId());
			qzmessage.setTitle("申请加入圈子");
			qzmessage.setType(8);
			qzmessage.setUser_id(qzCircle_member.getUser_id());
			qzmessage.setIs_read(0);
			if (isneedconfirm == 0) {// 不需要群主同意，直接加入圈子
				qzmessage.setContent(join_member.getC_nick() + "加入了" + apiU_circle.getCircle_name() + "圈子");
			} else {
				qzmessage.setContent(join_member.getC_nick() + "申请加入" + apiU_circle.getCircle_name() + "圈子");
			}
			messagelist.add(qzmessage);
			if (isneedconfirm == 0) {
				// 添加消息分组
				ApiU_user_friend_group apiU_user_friend_group = this.apiU_user_friend_groupMapper
						.findFriend_groupByName(joinuserid, "我的消息");
				ApiU_user_friend_group_detail apiU_user_friend_group_detail = new ApiU_user_friend_group_detail();
				apiU_user_friend_group_detail.setFriend_group_id(apiU_user_friend_group.getId().toString());
				apiU_user_friend_group_detail.setAdd_time(Util.CurrentTime());
				apiU_user_friend_group_detail.setFriend_id(circle_id);
				apiU_user_friend_group_detail.setType(1);
				this.apiU_user_friend_group_detailMapper
						.U_user_friend_group_detailInsertion(apiU_user_friend_group_detail);
			}
		}
		apiU_messageMapper.insertmessageList(messagelist);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "操作成功");
		return map;
	}

	@Transactional
	public Map<String, Object> auditjoinrequest(String circle_member_id, String status) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		ApiU_circle_member apiU_circle_member = apiU_circle_memberMapper
				.findU_circle_memberById(Integer.valueOf(circle_member_id));
		apiU_circle_member.setStatus(Integer.valueOf(status)); // 1同意，2拒绝
		apiU_circle_member.setUpdate_time(DateUtil.nowTimeToString());
		apiU_circle_member.setIs_deal(1);
		apiU_circle_memberMapper.updateU_circle_member(apiU_circle_member);

		if ("1".equals(status)) {
			// 添加消息分组
			ApiU_user_friend_group apiU_user_friend_group = this.apiU_user_friend_groupMapper
					.findFriend_groupByName(apiU_circle_member.getUser_id(), "我的消息");
			ApiU_user_friend_group_detail apiU_user_friend_group_detail = new ApiU_user_friend_group_detail();
			apiU_user_friend_group_detail.setFriend_group_id(apiU_user_friend_group.getId().toString());
			apiU_user_friend_group_detail.setAdd_time(Util.CurrentTime());
			apiU_user_friend_group_detail.setFriend_id(apiU_circle_member.getCircle_id());
			apiU_user_friend_group_detail.setType(1);
			this.apiU_user_friend_group_detailMapper.U_user_friend_group_detailInsertion(apiU_user_friend_group_detail);
		}

		// 审核结束后通知申请人
		ApiU_message message = new ApiU_message();
		message.setAdd_time(DateUtil.nowTimeToString());
		message.setConn_id(apiU_circle_member.getId());
		message.setTitle("圈主审核请求");
		message.setType(8);
		message.setUser_id(apiU_circle_member.getUser_id());
		message.setIs_read(0);
		if ("1".equals(status)) {
			message.setContent("圈主同意你的申请，您已加入该圈子");
		} else {
			message.setContent("圈主驳回你的申请");
		}
		apiU_messageMapper.U_messageInsertion(message);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "操作成功");

		return map;
	}

	// 删除成员
	@Transactional
	public Map<String, Object> delcirclemember(String delids) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		// 清除人员；
		if (delids != null && !"".equals(delids)) {
			map.clear();
			map.put("ids", delids.split(":"));
			map.put("status", 4); // 清除成员
			apiU_circle_memberMapper.updatecirclememberinfo(map);
		}
		// 清除人员和圈子的消息组
		String[] ids = delids.split(":");
		List<ApiU_message> messagelist = new ArrayList<ApiU_message>();
		ApiU_circle_member member = apiU_circle_memberMapper.findU_circle_memberById(Integer.valueOf(ids[0]));
		ApiU_circle apiU_circle = apiU_circleMapper.findU_circleById(Integer.valueOf(member.getCircle_id()));
		for (String id : ids) {
			ApiU_circle_member apiU_circle_member = apiU_circle_memberMapper
					.findU_circle_memberById(Integer.valueOf(id));
			ApiU_user_friend_group_detail detail = apiU_user_friend_group_detailMapper
					.findFriendGroupDetailByFriend(apiU_circle_member.getUser_id(), apiU_circle.getId() + "", 1);
			if (detail != null) {
				apiU_user_friend_group_detailMapper.deleteU_user_friend_group_detail(detail.getId());
			}

			// 消息
			ApiU_message message = new ApiU_message();
			message.setAdd_time(DateUtil.nowTimeToString());
			message.setConn_id(Integer.valueOf(apiU_circle_member.getUser_id()));
			message.setTitle("退出圈子");
			message.setType(8);
			message.setUser_id(apiU_circle_member.getUser_id());
			message.setIs_read(0);
			message.setContent(apiU_circle.getCircle_name() + "圈子的圈主将你移出圈子");
			messagelist.add(message);

		}
		apiU_messageMapper.insertmessageList(messagelist);
		map.clear();
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "操作成功");

		return map;
	}

	@Transactional
	public Map<String, Object> updatecirclemembersilent(String circle_id, String silentids) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		// 先将全部成员改成未禁言
		map.clear();
		map.put("is_silent", 0);
		map.put("circle_id", circle_id);
		apiU_circle_memberMapper.updatecirclememberinfo(map);
		// 禁言选中的成员
		if (silentids != null && !"".equals(silentids)) {
			map.clear();
			map.put("ids", silentids.split(":"));
			map.put("is_silent", 1);
			apiU_circle_memberMapper.updatecirclememberinfo(map);
		}
		map.clear();
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "操作成功");
		return map;
	}

	// 成员退出圈子
	public Map<String, Object> signoutcircle(String user_id, String circle_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", user_id);
		map.put("circle_id", circle_id);
		map.put("status", 1);
		ApiU_circle_member apiU_circle_member = apiU_circle_memberMapper.getcirclememberInfo(map);
		map.clear();
		if (apiU_circle_member != null) {
			apiU_circle_member.setStatus(3);
			apiU_circle_memberMapper.updateU_circle_member(apiU_circle_member);

			ApiU_user_friend_group_detail detail = apiU_user_friend_group_detailMapper
					.findFriendGroupDetailByFriend(apiU_circle_member.getUser_id(), circle_id, 1);
			if (detail != null) {
				apiU_user_friend_group_detailMapper.deleteU_user_friend_group_detail(detail.getId());
			}

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作成功");
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_NO_DATA);
			map.put(Utils.MESSAGE, "不是该圈子成员");
		}
		return map;
	}
}
